
import { _decorator, BlockInputEvents, Color, color, Component, game, Graphics, Node, UITransform, view } from 'cc';
import EventManager from '../../Runtime/EventManager';
import { CONTROLLER_ENUM, EVENT_ENUM } from '../../Enum';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = ControllerManager
 * DateTime = Tue Jun 27 2023 10:16:01 GMT+0800 (中国标准时间)
 * Author = zshzshzsh15
 * FileBasename = ControllerManager.ts
 * FileBasenameNoExtension = ControllerManager
 * URL = db://assets/Script/UI/ControllerManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */





const SCREEN_WIDTH=view.getVisibleSize().width

const SCREEN_HEIGHT=view.getVisibleSize().height

export const DEFAULT_DURATION=200

enum FADE_STATE_ENUM{
    IDLE='IDLE',
    FADE_IN='FADE_IN',
    FADE_OUT='FADE_OUT',
}


@ccclass('DrawManager')
export class DrawManager extends Component {

    private ctx: Graphics
    private state:FADE_STATE_ENUM =FADE_STATE_ENUM.IDLE
    private oldTime:number=0

    private duration:number=DEFAULT_DURATION

    private fadeResolve:(value: PromiseLike<null>) => void
    
    private block:BlockInputEvents
    init(){
        this.block=this.addComponent(BlockInputEvents)
        this.ctx =this.addComponent(Graphics)


        const tranform=this.getComponent(UITransform)
        tranform.setAnchorPoint(0.5,0.5)
        tranform.setContentSize(SCREEN_WIDTH,SCREEN_HEIGHT)
        
        this.setAlpha(1)  //初始化就填满他  纯黑
    }

    setAlpha(precent:number){
        this.ctx.clear()
        this.ctx.rect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)
        this.ctx.fillColor=new Color(0,0,0,255*precent)
        this.ctx.fill()
        this.block.enabled = precent ===1

    }


    //随着时间的流逝来改变透明度
    update(){
        const percent=(game.totalTime-this.oldTime)/this.duration  //当前的时间-旧时间/ 总的时间
        switch(this.state){
            case FADE_STATE_ENUM.FADE_IN:
                if(percent<1){
                    this.setAlpha(percent)
                }else{
                    this.state=FADE_STATE_ENUM.IDLE
                    this.setAlpha(1)
                    this.fadeResolve(null)
                }
                break
            case FADE_STATE_ENUM.FADE_OUT:
                if(percent<1){
                    this.setAlpha(1-percent)
                }else{
                    this.state=FADE_STATE_ENUM.IDLE
                    
                    this.setAlpha(0)
                    this.fadeResolve(null)
                }
                break

        }
    }

    fadeIn(duration:number=DEFAULT_DURATION){
        
        this.setAlpha(0)
        this.duration=duration
        this.oldTime=game.totalTime
        this.state=FADE_STATE_ENUM.FADE_IN

        return new Promise(resolve=>{
            this.fadeResolve=resolve
        })
    }


    fadeOut(duration:number=DEFAULT_DURATION){
        this.setAlpha(1)
        this.duration=duration
        this.oldTime=game.totalTime
        this.state=FADE_STATE_ENUM.FADE_OUT


        return new Promise(resolve=>{
            this.fadeResolve=resolve
        })
    }
}


